package com.zxjbyte.yiyi.module.plugin.job.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.plugin.job.domain.dto.JobLogAddDTO;
import com.zxjbyte.yiyi.module.plugin.job.domain.entity.JobLogPO;
import com.zxjbyte.yiyi.module.plugin.job.domain.response.JobLogPageResponse;
import com.zxjbyte.yiyi.module.plugin.job.mapper.JobLogMapper;
import com.zxjbyte.yiyi.module.plugin.job.service.IJobLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 调度日志操作处理
 * @Author zhangxingjia
 * @Date 2023/10/10 15:52
 * @Version: 1.0
 */
@Service
public class JobLogServiceImpl implements IJobLogService {

    @Resource
    private JobLogMapper jobLogMapper;

    @Override
    public PageResult<JobLogPageResponse> page(Integer status) {
        LambdaQueryWrapper<JobLogPO> wrapper = Wrappers.<JobLogPO>lambdaQuery().eq(Objects.nonNull(status), JobLogPO::getStatus, status);
        IPage<JobLogPO> page = jobLogMapper.selectPage(Condition.getSimplePage(), wrapper);
        List<JobLogPO> records = page.getRecords();
        if(CollUtil.isEmpty(records)){
            return PageResult.emptyOf((int)page.getCurrent(), (int)page.getSize());
        }
        List<JobLogPageResponse> responseList = CglibUtil.copyList(records, JobLogPageResponse::new);
        return PageResult.of(responseList, page);
    }

    @Override
    public void addLog(JobLogAddDTO addDto) {
        JobLogPO entity = new JobLogPO();
        CglibUtil.copy(addDto, entity);
        jobLogMapper.insert(entity);
    }
}
